package com.xieyong.leetcode;

import java.util.HashMap;
import java.util.Map;

public class Leetcode594 {
    public static void main(String[] args) {
        Leetcode594 leetcode594 = new Leetcode594();
        int[] nums = {1,3,2,2,5,2,3,7};
        int lhs = leetcode594.findLHS(nums);
        System.out.println(lhs);
    }

    public int findLHS(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i : nums) {
            map.put(i,map.getOrDefault(i,0)+1);
        }
        int maxlen = 0;
        for (int i : map.keySet()) {
            int value = map.get(i);
            int a = map.getOrDefault(i-1,0);
            int b = map.getOrDefault(i+1,0);
            if(a==0 && b==0)continue;
            maxlen = Math.max(maxlen,value+Math.max(a,b));
        }
        return maxlen;

    }
}
